package priv.happy.mealpickpro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import priv.happy.mealpickpro.domain.Address;
import priv.happy.mealpickpro.service.AddressService;
import priv.happy.mealpickpro.mapper.AddressMapper;
import org.springframework.stereotype.Service;
import priv.happy.mealpickpro.vo.AddressVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HAPPY
 * @description 针对表【address】的数据库操作Service实现
 * @createDate 2023-10-21 10:14:03
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address>
        implements AddressService {
    @Autowired
    AddressMapper addressMapper;

    @Override
    public List<AddressVO> getAddressListByUserId(Integer userId) {
        List<AddressVO> addressVOList = new ArrayList<>();
        List<Address> addressList = list(new QueryWrapper<Address>()
                .eq("user_id", userId)
                .orderByDesc("status"));
        for (Address address : addressList) {
            addressVOList.add(new AddressVO().setId(address.getId()).setTel(address.getUserEmail())
                    .setName(address.getUserRole()).setAddress(address.getAddress()).setStatus(address.getStatus()));
        }

        return addressVOList;
    }

    @Override
    public AddressVO getDefaultByUserId(Integer userId) {
        Address address = getOne(new QueryWrapper<Address>().eq("user_id", userId).eq("status", 1));
        return new AddressVO().setId(address.getId()).setTel(address.getUserEmail())
                .setName(address.getUserRole()).setAddress(address.getAddress()).setStatus(address.getStatus());
    }

    @Override
    public boolean removeUserAddress(Integer userId, Integer id) {
        return remove(new QueryWrapper<Address>().eq("id", id).eq("user_id", userId));
    }

    @Override
    public boolean updateAddressById(Address address) {
        boolean update = update(new UpdateWrapper<Address>()
                .set("address", address.getAddress())
                .eq("id", address.getId()).eq("user_id", address.getUserId()));
        return update;
    }

    @Override
    public boolean updateDefaultAddressById(Address address) {

        // 取消默认地址设置
        boolean updateOld = update(new UpdateWrapper<Address>()
                .set("status", 0)
                .eq("user_id", address.getUserId())
                .eq("status", 1));

        // 设置为默认地址
        boolean updateNew = update(new UpdateWrapper<Address>()
                .set("status", 1)
                .eq("id", address.getId())
                .eq("user_id", address.getUserId()));

        return updateOld && updateNew;
    }

    @Override
    public boolean isFirst(Integer userId) {
        return addressMapper.countUserAddress(userId) == 0;
    }
}




